package com.polaris.live.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R.drawable
import com.polaris.live.common.R.*
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.databinding.AdapterPartyGamePlayBinding
import com.polaris.live.resp.back_resp.PartyGameSettlementResp
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.avatar.FrameImageUtils

/**
 * 结算界面
 * @author Created by 天晴 on 2024/3/15 11:30
 * @since 1.0.0
 **/
class PartyGamePlayAdapter :
    BaseAdapter<PartyGameSettlementResp, PartyGamePlayAdapter.PartyGamePlayHolder>(0) {

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PartyGamePlayHolder {
        return PartyGamePlayHolder(
            AdapterPartyGamePlayBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
    }

    override fun convert(holder: PartyGamePlayHolder, item: PartyGameSettlementResp) {
        val userBasic = item.base
        holder.binding.name.text = userBasic.nickname
        holder.binding.name.setNicknameColor(
            userBasic.nicknameColor,
            color.color_202028
        )
        when (item.rank) {
            1L -> {
                holder.binding.rankImg.visibility = View.VISIBLE
                holder.binding.rankTv.visibility = View.GONE
                holder.binding.rankImg.setImageResource(drawable.party_game_no1)
            }

            2L -> {
                holder.binding.rankImg.visibility = View.VISIBLE
                holder.binding.rankTv.visibility = View.GONE
                holder.binding.rankImg.setImageResource(drawable.party_game_no2)
            }

            3L -> {
                holder.binding.rankImg.visibility = View.VISIBLE
                holder.binding.rankTv.visibility = View.GONE
                holder.binding.rankImg.setImageResource(drawable.party_game_no3)
            }

            else -> {
                holder.binding.rankImg.visibility = View.GONE
                holder.binding.rankTv.visibility = View.VISIBLE
                holder.binding.rankTv.text = item.rank.toString()
            }
        }
        val coin = item.coin
        if (coin == 0L) {
            holder.binding.endLl.visibility = View.GONE
            holder.binding.freeImg.visibility = View.VISIBLE
        } else {
            holder.binding.endLl.visibility = View.VISIBLE
            holder.binding.freeImg.visibility = View.GONE
            if (ViewCompat.getLayoutDirection(holder.binding.money) == ViewCompat.LAYOUT_DIRECTION_LTR) {
                if (item.winner) {
                    holder.binding.money.text = "+ $coin"
                } else {
                    holder.binding.money.text = "- $coin"
                }

            } else {
                if (item.winner) {
                    holder.binding.money.text = "$coin +"
                } else {
                    holder.binding.money.text = "$coin -"
                }

            }

            Glide.with(context)
                .load(ImageUtils.imgPath(item.assetIcon))
                .into(holder.binding.diamond)
        }

        holder.binding.acFv.setFrameImage(FrameImageUtils.urlToFrameImageBean(userBasic.avatarFrame))
        Glide.with(context)
            .load(ImageUtils.imgPathSmall(userBasic.avatar))
            .transform(CircleCrop())
            .into(holder.binding.avater)
        holder.binding.cvCountry.setCountry(userBasic.country)
        holder.binding.saSex.setSexAndAge(userBasic.sex, userBasic.birthday)
        holder.binding.vipView.setVipLevel(userBasic.vipLevel, 64.autoDp, 20.autoDp)
        holder.binding.level1.setRichCharmBySexLevel(
            userBasic.sex,
            userBasic.richs,
            userBasic.charms
        )
    }

    inner class PartyGamePlayHolder(val binding: AdapterPartyGamePlayBinding) :
        BaseViewHolder(binding.root)
}